\clearpage

# Users-URLs and Users-Domains networks

```{r include = T, results = "asis"}

require(igraph)

bushfire_url.el <-
  bushfire_urls.dt[,c('tweet_id','clean_url')]

bushfire_url.el$user_id <- 
  paste0("user", bushfire_tweets.dt$user_id[match(bushfire_url.el$tweet_id,
                                                  bushfire_tweets.dt$tweet_id)])
bushfire_url.g <- 
  graph_from_data_frame(bushfire_url.el[,c("user_id",'clean_url')])

V(bushfire_url.g)$type <- 
  grepl("^user", V(bushfire_url.g)$name)

bushfire_url_proj.g <-
  igraph::bipartite_projection(bushfire_url.g, which = 'true')

data.frame(nodes = c(vcount(bushfire_url.g), vcount(bushfire_url_proj.g)),
           edges = c(ecount(bushfire_url.g), ecount(bushfire_url_proj.g)),
           type = c("user-url", "user-user (projected)")) %>%
  kbl(format = "latex", caption = "Period 1: User-URLs network",
      booktabs = T) %>%
  kable_styling(latex_options = "HOLD_position")

```

```{r include = T, results = "asis"}

require(igraph)

covid_url.el <-
  covid_urls.dt[,c('tweet_id','clean_url')]

covid_url.el$user_id <- 
  paste0("user", covid_tweets.dt$user_id[match(covid_url.el$tweet_id,
                                                  covid_tweets.dt$tweet_id)])
covid_url.g <- 
  graph_from_data_frame(covid_url.el[,c("user_id",'clean_url')])

V(covid_url.g)$type <- 
  grepl("^user", V(covid_url.g)$name)

covid_url_proj.g <-
  igraph::bipartite_projection(covid_url.g, which = 'true')

data.frame(nodes = c(vcount(covid_url.g), vcount(covid_url_proj.g)),
           edges = c(ecount(covid_url.g), ecount(covid_url_proj.g)),
           type = c("user-url", "user-user (projected)")) %>%
  kbl(format = "latex", caption = "Period 2: User-URLs network",
      booktabs = T) %>%
  kable_styling(latex_options = "HOLD_position")

```

```{r newsmedia_g, include = T, fig.width = 10, fig.height = 10, fig.cap = "Relation among top web domains as they are co-linked from tweets.", cache = TRUE}

load("data/reuters_2020_au_news_lr.df.RData")

bushfire_user_domain.el <- 
  bushfire_tweets.dt %>%
  dplyr::select(user_id, tweet_id, created_at) %>%
  right_join(bushfire_urls.dt, 
             by = c(tweet_id = "tweet_id")) %>%
  dplyr::mutate(domain = gsub("www.", "", domain)) %>%
  dplyr::filter(!is.na(domain))

bushfire_user_domain.g <-
  graph_from_data_frame(bushfire_user_domain.el[,c('user_id','domain')])

V(bushfire_user_domain.g)$type <- 
  V(bushfire_user_domain.g)$name %in% 
  bushfire_user_domain.el$user_id

V(bushfire_user_domain.g)$indegree <- 
  degree(bushfire_user_domain.g, mode = "in")

V(bushfire_user_domain.g)$outdegree <- 
  degree(bushfire_user_domain.g, mode = "out")

V(bushfire_user_domain.g)$news_source <- 
  australia_news_sources$collection[match(V(bushfire_user_domain.g)$name,
                                          australia_news_sources$domain)]

bushfire_user_domain_indegree.df <-
  data.frame(name = V(bushfire_user_domain.g)$name,
             indegree = V(bushfire_user_domain.g)$indegree,
             news_source = V(bushfire_user_domain.g)$news_source) %>%
  # dplyr::filter(!is.na(news_source)) %>%
  dplyr::arrange(desc(indegree)) %>%
  dplyr::mutate(rank = 1:n())

E(bushfire_user_domain.g)$weight <- 1

bushfire_user_domain.g <-
  igraph::simplify(bushfire_user_domain.g)

V(bushfire_user_domain.g)$lr <- 
  reuters_2020_au_news_lr.df$lr[match(V(bushfire_user_domain.g)$name,
                                      tolower(reuters_2020_au_news_lr.df$domain))]

bushfire_user_domain_lr_proj.g <- 
  igraph::bipartite.projection(bushfire_user_domain.g, which = "false")

### Far-right

bushfire_user_domain_farright.g <-
  graph_from_data_frame(bushfire_user_domain.el[bushfire_user_domain.el$user_id %in% 
                                                  farright_user_ids,c('user_id','domain')])

V(bushfire_user_domain_farright.g)$type <- 
  V(bushfire_user_domain_farright.g)$name %in% 
  bushfire_user_domain.el$user_id

V(bushfire_user_domain_farright.g)$indegree <- 
  degree(bushfire_user_domain_farright.g, mode = "in")

V(bushfire_user_domain_farright.g)$outdegree <- 
  degree(bushfire_user_domain_farright.g, mode = "out")

V(bushfire_user_domain_farright.g)$news_source <- 
  australia_news_sources$collection[match(V(bushfire_user_domain_farright.g)$name,
                                          australia_news_sources$domain)]

bushfire_user_domain_farright_indegree.df <-
  data.frame(name = V(bushfire_user_domain_farright.g)$name,
             indegree = V(bushfire_user_domain_farright.g)$indegree,
             news_source = V(bushfire_user_domain_farright.g)$news_source) %>%
  # dplyr::filter(!is.na(news_source)) %>%
  dplyr::arrange(desc(indegree)) %>%
  dplyr::mutate(rank = 1:n())

E(bushfire_user_domain_farright.g)$weight <- 1

bushfire_user_domain_farright.g <-
  igraph::simplify(bushfire_user_domain_farright.g)

V(bushfire_user_domain_farright.g)$lr <- 
  reuters_2020_au_news_lr.df$lr[match(V(bushfire_user_domain_farright.g)$name,
                                      tolower(reuters_2020_au_news_lr.df$domain))]

bushfire_user_domain_lr_proj_farright.g <- 
  igraph::bipartite.projection(bushfire_user_domain_farright.g, which = "false")

## Covid

covid_user_domain.el <- 
  covid_tweets.dt %>%
  dplyr::select(user_id, tweet_id, created_at) %>%
  right_join(covid_urls.dt, 
             by = c(tweet_id = "tweet_id")) %>%
  dplyr::mutate(domain = gsub("www.", "", domain)) %>%
  dplyr::filter(!is.na(domain))

covid_user_domain.g <-
  graph_from_data_frame(covid_user_domain.el[,c('user_id','domain')])

V(covid_user_domain.g)$type <- 
  V(covid_user_domain.g)$name %in% 
  covid_user_domain.el$user_id

V(covid_user_domain.g)$indegree <- 
  degree(covid_user_domain.g, mode = "in")

V(covid_user_domain.g)$outdegree <- 
  degree(covid_user_domain.g, mode = "out")

V(covid_user_domain.g)$news_source <- 
  australia_news_sources$collection[match(V(covid_user_domain.g)$name,
                                          australia_news_sources$domain)]

covid_user_domain_indegree.df <-
  data.frame(name = V(covid_user_domain.g)$name,
             indegree = V(covid_user_domain.g)$indegree,
             news_source = V(covid_user_domain.g)$news_source) %>%
  # dplyr::filter(!is.na(news_source)) %>%
  dplyr::arrange(desc(indegree)) %>%
  dplyr::mutate(rank = 1:n())

E(covid_user_domain.g)$weight <- 1

covid_user_domain.g <-
  igraph::simplify(covid_user_domain.g)

V(covid_user_domain.g)$lr <- 
  reuters_2020_au_news_lr.df$lr[match(V(covid_user_domain.g)$name,
                                      tolower(reuters_2020_au_news_lr.df$domain))]

covid_user_domain_lr_proj.g <- 
  igraph::bipartite.projection(covid_user_domain.g, which = "false")

### Far-right

covid_user_domain_farright.g <-
  graph_from_data_frame(covid_user_domain.el[covid_user_domain.el$user_id %in% 
                                               farright_user_ids,c('user_id','domain')])

V(covid_user_domain_farright.g)$type <- 
  V(covid_user_domain_farright.g)$name %in% 
  covid_user_domain.el$user_id

V(covid_user_domain_farright.g)$indegree <- 
  degree(covid_user_domain_farright.g, mode = "in")

V(covid_user_domain_farright.g)$outdegree <- 
  degree(covid_user_domain_farright.g, mode = "out")

V(covid_user_domain_farright.g)$news_source <- 
  australia_news_sources$collection[match(V(covid_user_domain_farright.g)$name,
                                          australia_news_sources$domain)]

covid_user_domain_farright_indegree.df <-
  data.frame(name = V(covid_user_domain_farright.g)$name,
             indegree = V(covid_user_domain_farright.g)$indegree,
             news_source = V(covid_user_domain_farright.g)$news_source) %>%
  # dplyr::filter(!is.na(news_source)) %>%
  dplyr::arrange(desc(indegree)) %>%
  dplyr::mutate(rank = 1:n())

E(covid_user_domain_farright.g)$weight <- 1

covid_user_domain_farright.g <-
  igraph::simplify(covid_user_domain_farright.g)

V(covid_user_domain_farright.g)$lr <- 
  reuters_2020_au_news_lr.df$lr[match(V(covid_user_domain_farright.g)$name,
                                      tolower(reuters_2020_au_news_lr.df$domain))]

covid_user_domain_lr_proj_farright.g <- 
  igraph::bipartite.projection(covid_user_domain_farright.g, which = "false")

### Politician

covid_user_domain_politician.g <-
  graph_from_data_frame(covid_user_domain.el[covid_user_domain.el$user_id %in% 
                                               politician_user_ids,c('user_id','domain')])

V(covid_user_domain_politician.g)$type <- 
  V(covid_user_domain_politician.g)$name %in% 
  covid_user_domain.el$user_id

V(covid_user_domain_politician.g)$indegree <- 
  degree(covid_user_domain_politician.g, mode = "in")

V(covid_user_domain_politician.g)$outdegree <- 
  degree(covid_user_domain_politician.g, mode = "out")

V(covid_user_domain_politician.g)$news_source <- 
  australia_news_sources$collection[match(V(covid_user_domain_politician.g)$name,
                                          australia_news_sources$domain)]

covid_user_domain_politician_indegree.df <-
  data.frame(name = V(covid_user_domain_politician.g)$name,
             indegree = V(covid_user_domain_politician.g)$indegree,
             news_source = V(covid_user_domain_politician.g)$news_source) %>%
  # dplyr::filter(!is.na(news_source)) %>%
  dplyr::arrange(desc(indegree)) %>%
  dplyr::mutate(rank = 1:n())

E(covid_user_domain_politician.g)$weight <- 1

covid_user_domain_politician.g <-
  igraph::simplify(covid_user_domain_politician.g)

V(covid_user_domain_politician.g)$lr <- 
  reuters_2020_au_news_lr.df$lr[match(V(covid_user_domain_politician.g)$name,
                                      tolower(reuters_2020_au_news_lr.df$domain))]

covid_user_domain_lr_proj_politician.g <- 
  igraph::bipartite.projection(covid_user_domain_politician.g, which = "false")

right_left_scale_limits <-
  c(min(reuters_2020_au_news_lr.df$lr),
    max(reuters_2020_au_news_lr.df$lr))

legend <- cowplot::get_legend(
  
          ggnetwork(bushfire_user_domain_lr_proj.g %>%
                     igraph::delete_vertices(!V(bushfire_user_domain_lr_proj.g)$name %in%
                                               bushfire_user_domain_indegree.df$name[
                                                 bushfire_user_domain_indegree.df$rank < 25 &
                                                   !bushfire_user_domain_indegree.df$name %in% 
                                                   c("twitter.com", "paper.li")
                                               ])) %>%
           ggplot(aes(x = x, y = y, xend = xend, yend = yend)) +
           geom_edges(color = "grey90", size = .5) +
           geom_nodes(aes(color = lr, size = sqrt(indegree))) +
           geom_nodelabel_repel(aes(label = name),
                                max.overlaps = 50, 
                                size = 2.8) +
           scale_colour_distiller(palette = "RdYlBu", limits = right_left_scale_limits) +
           scale_size_continuous(range = c(2,11)) +
           guides(size = 'none') +
           theme_void() +
           theme(legend.position = 'bottom') +
           guides(color = guide_colourbar(title = "<= left  right =>", 
                                        title.position="top", 
                                        title.hjust = 0.5,
                                        labels = NULL))
)

       
       cowplot::plot_grid(
         
         ggnetwork(bushfire_user_domain_lr_proj.g %>%
                     igraph::delete_vertices(!V(bushfire_user_domain_lr_proj.g)$name %in%
                                               bushfire_user_domain_indegree.df$name[
                                                 bushfire_user_domain_indegree.df$rank < 25 &
                                                   !bushfire_user_domain_indegree.df$name %in% 
                                                   c("twitter.com", "paper.li")
                                               ])) %>%
           ggplot(aes(x = x, y = y, xend = xend, yend = yend)) +
           geom_edges(color = "grey90", size = .5) +
           geom_nodes(aes(color = lr, size = sqrt(indegree))) +
           geom_nodelabel_repel(aes(label = name),
                                max.overlaps = 50, 
                                size = 2.8) +
           scale_colour_distiller(palette = "RdYlBu", limits = right_left_scale_limits) +
           scale_size_continuous(range = c(2,11)) +
           guides(size = 'none', colour = 'none') +
           theme_void(),
         
         ggnetwork(covid_user_domain_lr_proj.g %>%
                     igraph::delete_vertices(!V(covid_user_domain_lr_proj.g)$name %in%
                                               covid_user_domain_indegree.df$name[
                                                 covid_user_domain_indegree.df$rank < 25 &
                                                   !covid_user_domain_indegree.df$name %in% 
                                                   c("twitter.com", "paper.li")
                                               ])) %>%
           ggplot(aes(x = x, y = y, xend = xend, yend = yend)) +
           geom_edges(color = "grey90", size = .5) +
           geom_nodes(aes(color = lr, size = sqrt(indegree))) +
           geom_nodelabel_repel(aes(label = name),
                                max.overlaps = 50, 
                                size = 2.8) +
           scale_colour_distiller(palette = "RdYlBu", limits = right_left_scale_limits) +
           scale_size_continuous(range = c(2,8)) +
           guides(colour = 'none', size = 'none') +
           theme_void(),
         
         ggnetwork(bushfire_user_domain_lr_proj_farright.g %>%
                     igraph::delete_vertices(!V(bushfire_user_domain_lr_proj_farright.g)$name %in%
                                               bushfire_user_domain_farright_indegree.df$name[
                                                 bushfire_user_domain_farright_indegree.df$rank < 25 &
                                                   !bushfire_user_domain_farright_indegree.df$name %in% 
                                                   c("twitter.com", "paper.li")
                                               ])) %>%
           ggplot(aes(x = x, y = y, xend = xend, yend = yend)) +
           geom_edges(color = "grey90", size = .5) +
           geom_nodes(aes(color = lr, size = sqrt(indegree))) +
           geom_nodelabel_repel(aes(label = name),
                                max.overlaps = 50, 
                                size = 2.8) +
           scale_colour_distiller(palette = "RdYlBu", limits = right_left_scale_limits) +
           scale_size_continuous(range = c(2,11)) +
           guides(colour = 'none', size = 'none') +
           theme_void(),
         
         ggnetwork(covid_user_domain_lr_proj_farright.g %>%
                     igraph::delete_vertices(!V(covid_user_domain_lr_proj_farright.g)$name %in%
                                               covid_user_domain_farright_indegree.df$name[
                                                 covid_user_domain_farright_indegree.df$rank < 25 &
                                                   !covid_user_domain_farright_indegree.df$name %in% 
                                                   c("twitter.com", "paper.li")
                                               ])) %>%
           ggplot(aes(x = x, y = y, xend = xend, yend = yend)) +
           geom_edges(color = "grey90", size = .5) +
           geom_nodes(aes(color = lr, size = sqrt(indegree))) +
           geom_nodelabel_repel(aes(label = name),
                                max.overlaps = 50, 
                                size = 2.8) +
           scale_colour_distiller(palette = "RdYlBu", limits = right_left_scale_limits) +
           scale_size_continuous(range = c(2,11)) +
           guides(colour = 'none', size = 'none') +
           theme_void(),
         
         
         # ggnetwork(covid_user_domain_lr_proj_politician.g %>%
         #             igraph::delete_vertices(!V(covid_user_domain_lr_proj_politician.g)$name %in%
         #                                       covid_user_domain_politician_indegree.df$name[
         #                                         covid_user_domain_politician_indegree.df$rank < 51 &
         #                                           !covid_user_domain_politician_indegree.df$name %in% 
         #                                           c("twitter.com", "paper.li")
         #                                       ])) %>%
         #   ggplot(aes(x = x, y = y, xend = xend, yend = yend)) +
         #   geom_edges(color = "grey90", size = .5) +
         #   geom_nodes(aes(color = lr, size = sqrt(indegree))) +
         #   geom_nodelabel_repel(aes(label = name),
         #                        max.overlaps = 50, 
         #                        size = 2.8) +
         #   scale_colour_distiller(palette = "RdYlBu", limits = right_left_scale_limits) +
         #   scale_size_continuous(range = c(2,11)) +
         #   guides(colour = 'none', size = 'none') +
         #   theme_void(),
         
         legend,
         
         ncol = 2, labels = c("Bushfires", "Covid-19", "Bushfires (far-right only)", "Covid-19 (far-right only)", ""), rel_heights = c(1, 1, .15))

```

```{r include = T, results = "asis"}

data.frame(nodes = c(vcount(bushfire_user_domain.g), vcount(bushfire_user_domain_lr_proj.g)),
           edges = c(ecount(bushfire_user_domain.g), ecount(bushfire_user_domain_lr_proj.g)),
           type = c("user-domain", "user-user (projected)")) %>%
  kbl(format = "latex", caption = "Period 1: User-Domain network",
      booktabs = T)

```

```{r include = T, results = "asis"}

data.frame(nodes = c(vcount(covid_user_domain.g), vcount(covid_user_domain_lr_proj.g)),
           edges = c(ecount(covid_user_domain.g), ecount(covid_user_domain_lr_proj.g)),
           type = c("user-domain", "user-user (projected)")) %>%
  kbl(format = "latex", caption = "Period 2: User-Domain network",
      booktabs = T)

```

```{r include = T, results = "asis"}

bushfire_url.el <-
  bushfire_urls.dt[,c('tweet_id','clean_url')]

bushfire_url.el$user_id <- 
  paste0("user", bushfire_tweets.dt$user_id[match(bushfire_url.el$tweet_id,
                                                  bushfire_tweets.dt$tweet_id)])
bushfire_url.g <- 
  graph_from_data_frame(bushfire_url.el[,c("user_id",'clean_url')])

V(bushfire_url.g)$type <- 
  grepl("^user", V(bushfire_url.g)$name)

covid_url.el <-
  covid_urls.dt[,c('tweet_id','clean_url')]

covid_url.el$user_id <- 
  paste0("user", covid_tweets.dt$user_id[match(covid_url.el$tweet_id,
                                               covid_tweets.dt$tweet_id)])
covid_url.g <- 
  graph_from_data_frame(covid_url.el[,c("user_id",'clean_url')])

V(covid_url.g)$type <- 
  grepl("^user", V(covid_url.g)$name)

load("data/url_proj.c.RData")

load("data/coded_opinions_twt_users.RData")

bushfire_url_proj_c.df <- 
  data.frame(user_id = gsub("user", "", bushfire_url_proj.c$names),
             community = bushfire_url_proj.c$membership) %>%
  dplyr::mutate(far_right = user_id %in% farright_user_ids,
                journalist = user_id %in% journalist_user_ids,
                politician = user_id %in% politician_user_ids,
                lr = bushfire_user_lr.df$lr_reuters_2020[match(user_id, 
                                                               bushfire_user_lr.df$user_id)],
                problematic_opinion = user_id %in% problematic_speech_twt_usr,
                conspiracy_opinion = user_id %in% conspiracy_theory_twt_usr,
                misinformation_opinion = user_id %in% misinformation_twt_usr)

covid_url_proj_c.df <- 
  data.frame(user_id = gsub("user", "", covid_url_proj.c$names),
             community = covid_url_proj.c$membership) %>%
  dplyr::mutate(far_right = user_id %in% farright_user_ids,
                journalist = user_id %in% journalist_user_ids,
                politician = user_id %in% politician_user_ids,
                lr = covid_user_lr.df$lr_reuters_2020[match(user_id, 
                                                            covid_user_lr.df$user_id)],
                problematic_opinion = user_id %in% problematic_speech_twt_usr,
                conspiracy_opinion = user_id %in% conspiracy_theory_twt_usr,
                misinformation_opinion = user_id %in% misinformation_twt_usr)

bushfire_url_target_egos.list <- 
  bushfire_url.g %>%
  igraph::ego(order = 1, nodes = 
                V(bushfire_url.g)$name %in% bushfire_url_proj.c$names[bushfire_url_proj.c$membership == 3])

bushfire_url_target_community.g <-
  bushfire_url.g %>%
  igraph::induced_subgraph(vids = unique(unlist(bushfire_url_target_egos.list)))

V(bushfire_url_target_community.g)$indegree <- 
  degree(bushfire_url_target_community.g, mode = 'in')

V(bushfire_url_target_community.g)$far_right <- 
  gsub("user", "", V(bushfire_url_target_community.g)$name) %in% farright_user_ids

bushfire_url_target_community.df <- 
  data.frame(name = V(bushfire_url_target_community.g)$name,
             indegree = V(bushfire_url_target_community.g)$indegree)

res_1 <- 
  degree(bushfire_url_target_community.g %>%
           delete_vertices(V(bushfire_url_target_community.g)$far_right), mode = 'in')

res_2 <- 
  degree(bushfire_url_target_community.g %>%
           delete_vertices(V(bushfire_url_target_community.g)$type == TRUE &
                             !V(bushfire_url_target_community.g)$far_right), mode = 'in')

bushfire_url_target_community.df$indegree_rest <- 
  res_1[match(bushfire_url_target_community.df$name,
              names(res_1))]

bushfire_url_target_community.df$indegree_far_right <- 
  res_2[match(bushfire_url_target_community.df$name,
              names(res_2))]

bushfire_url_target_community.df$indegree_far_right_perc <- 
  bushfire_url_target_community.df$indegree_far_right / 102

bushfire_url_target_community.df$indegree_rest_perc <- 
  bushfire_url_target_community.df$indegree_rest / 11023

bushfire_url_far_right_top_50.df <-
  bushfire_url_target_community.df %>%
  dplyr::top_n(50, indegree_far_right_perc)  %>%
  dplyr::arrange(desc(indegree_far_right_perc)) %>%
  dplyr::mutate(URL = name,
                `indegree (rest)` = indegree_rest, 
                `%, rest` = round(indegree_rest_perc*100,2),
                `indegree (far-right)` = indegree_far_right,
                `%, far-right` = round(indegree_far_right_perc*100,2)) %>%
  dplyr::select(URL:`%, far-right`) 

bushfire_url_far_right_top_50.df %>%
  kbl(format = "latex", caption = "Period 1: Top 50 URLs shared by far-right accounts part of the largest right wing cluster with number and proportion of member accounts (far-right or rest) that have been linking to that resource.",
                       booktabs = T,
        longtable = T) %>%
  kable_styling(latex_options = c("striped", "repeat_header"), font_size = 8) %>%
  column_spec(1, width = "25em")

bushfire_url_rest_top_50.df <-
  bushfire_url_target_community.df %>%
  dplyr::top_n(50, indegree_rest_perc) %>%
  dplyr::arrange(desc(indegree_rest_perc)) %>%
  dplyr::mutate(URL = name,
                `indegree (rest)` = indegree_rest, 
                `%, rest` = round(indegree_rest_perc*100,2),
                `indegree (far-right)` = indegree_far_right,
                `%, far-right` = round(indegree_far_right_perc*100,2)) %>%
  dplyr::select(URL:`%, far-right`) 

bushfire_url_rest_top_50.df %>%
  kbl(format = "latex", caption = "Period 1: Top 50 URLs shared by non-far-right accounts part of the largest right wing cluster with number and proportion of member accounts (far-right or rest) that have been linking to that resource.",
      booktabs = T, longtable = T) %>%
  kable_styling(latex_options = c("striped", "repeat_header"), font_size = 8) %>%
  column_spec(1, width = "25em")

data.frame(intersection = intersect(bushfire_url_far_right_top_50.df$URL, 
                                    bushfire_url_rest_top_50.df$URL)) %>%
  kbl(format = "latex", caption = "Period 1: Intersection of the two sets of top 50 URLs shared within the largest right wing cluster by far-right and non-far-right accounts.",
      booktabs = T, longtable = T) %>%
  kable_styling(latex_options = c("striped", "repeat_header"), font_size = 8) %>%
  column_spec(1, width = "45em")
  

# Covid

covid_url_target_egos.list <- 
  covid_url.g %>%
  igraph::ego(order = 1, nodes = 
                V(covid_url.g)$name %in% covid_url_proj.c$names[covid_url_proj.c$membership == 1])

covid_url_target_community.g <-
  covid_url.g %>%
  igraph::induced_subgraph(vids = unique(unlist(covid_url_target_egos.list)))

V(covid_url_target_community.g)$indegree <- 
  degree(covid_url_target_community.g, mode = 'in')

V(covid_url_target_community.g)$far_right <- 
  gsub("user", "", V(covid_url_target_community.g)$name) %in% farright_user_ids

# table(V(covid_url_target_community.g)$far_right,
#       V(covid_url_target_community.g)$type)

covid_url_target_community.df <- 
  data.frame(name = V(covid_url_target_community.g)$name,
             indegree = V(covid_url_target_community.g)$indegree)

res_1 <- 
  degree(covid_url_target_community.g %>%
           delete_vertices(V(covid_url_target_community.g)$far_right), mode = 'in')

res_2 <- 
  degree(covid_url_target_community.g %>%
           delete_vertices(V(covid_url_target_community.g)$type == TRUE &
                             !V(covid_url_target_community.g)$far_right), mode = 'in')

covid_url_target_community.df$indegree_rest <- 
  res_1[match(covid_url_target_community.df$name,
              names(res_1))]

covid_url_target_community.df$indegree_far_right <- 
  res_2[match(covid_url_target_community.df$name,
              names(res_2))]

covid_url_target_community.df$indegree_far_right_perc <- 
  covid_url_target_community.df$indegree_far_right / 122

covid_url_target_community.df$indegree_rest_perc <- 
  covid_url_target_community.df$indegree_rest / 10982

covid_url_far_right_top_50.df <-
  covid_url_target_community.df %>%
  dplyr::top_n(50, indegree_far_right_perc)  %>%
  dplyr::arrange(desc(indegree_far_right_perc)) %>%
  dplyr::mutate(URL = name,
                `indegree (rest)` = indegree_rest, 
                `%, rest` = round(indegree_rest_perc*100,2),
                `indegree (far-right)` = indegree_far_right,
                `%, far-right` = round(indegree_far_right_perc*100,2)) %>%
  dplyr::select(URL:`%, far-right`) 

covid_url_far_right_top_50.df %>%
  kable(format = "latex", caption = "Period 2: Top 50 URLs shared by far-right accounts part of the largest right wing cluster with number and proportion of member accounts (far-right or rest) that have been linking to that resource.",
                       booktabs = T,
        longtable = T) %>%
  kable_styling(latex_options = c("striped", "repeat_header"), font_size = 8) %>%
  column_spec(1, width = "25em")

covid_url_rest_top_50.df <-
  covid_url_target_community.df %>%
  dplyr::top_n(50, indegree_rest_perc) %>%
  dplyr::arrange(desc(indegree_rest_perc)) %>%
  dplyr::mutate(URL = name,
                `indegree (rest)` = indegree_rest, 
                `%, rest` = round(indegree_rest_perc*100,2),
                `indegree (far-right)` = indegree_far_right,
                `%, far-right` = round(indegree_far_right_perc*100,2)) %>%
  dplyr::select(URL:`%, far-right`)

bushfire_url_rest_top_50.df %>%
  kbl(format = "latex", caption = "Period 2: Top 50 URLs shared by non-far-right accounts part of the largest right wing cluster with number and proportion of member accounts (far-right or rest) that have been linking to that resource.",
                       booktabs = T, longtable = T) %>%
  kable_styling(latex_options = c("striped", "repeat_header"), font_size = 8) %>%
  column_spec(1, width = "25em")


data.frame(intersection = intersect(bushfire_url_far_right_top_50.df$URL, 
                                    bushfire_url_rest_top_50.df$URL)) %>%
  kbl(format = "latex", caption = "Period 1: Intersection of the two sets of top 50 URLs shared within the largest right wing cluster by far-right and non-far-right accounts.",
      booktabs = T, longtable = T) %>%
  kable_styling(latex_options = c("striped", "repeat_header"), font_size = 8) %>%
  column_spec(1, width = "45em")



bushfire_url_target_community.df$domain <- 
  gsub("http(s)?://|www\\.|/", "", domain(bushfire_url_target_community.df$name))

bushfire_url_target_community.df$legacy <- 
  bushfire_url_target_community.df$domain %in% australia_news_sources$domain


covid_url_target_community.df$domain <- 
  gsub("http(s)?://|www\\.|/", "", domain(covid_url_target_community.df$name))

covid_url_target_community.df$legacy <- 
  covid_url_target_community.df$domain %in% australia_news_sources$domain

# 

bushfire_url.el$domain <- 
  domain(bushfire_url.el$clean_url)

bushfire_url.el$legacy <- 
  bushfire_url.el$domain %in% australia_news_sources$domain

# bushfire_url.el %>%
#   dplyr::group_by(legacy) %>%
#   dplyr::count()

covid_url.el$domain <- 
  domain(covid_url.el$clean_url)

covid_url.el$legacy <- 
  covid_url.el$domain %in% australia_news_sources$domain

# covid_url.el %>%
#   dplyr::group_by(legacy) %>%
#   dplyr::count()

```

\clearpage

```{r cluster_urls, include = T,  fig.width = 10, fig.height = 10, fig.cap = "Community of users based on the URLs shared in their tweets with each red dot indicating a far-right account.", cache = T}

bushfire_comm_ggplot_df <- 
  bushfire_url_proj_c.df %>%
  dplyr::group_by(community) %>%
  dplyr::summarize(n = n(),
                   far_right_n = sum(far_right),
                   far_right_perc = far_right_n / n,
                   far_right_problematic_perc = sum(far_right & problematic_opinion)  / far_right_n,
                   journalist_n = sum(journalist),
                   journalist_perc = journalist_n / n,
                   journalist_problematic_perc = sum(journalist & problematic_opinion)  / journalist_n,
                   politician_n = sum(politician),
                   politician_perc = politician_n / n,
                   politician_problematic_perc = sum(politician & problematic_opinion)  / journalist_n,
                   problematic_n = sum(problematic_opinion),
                   problematic_perc = problematic_n / n,
                   lr_mean = mean(lr, na.rm = T)) %>%
  dplyr::filter(is.finite(lr_mean) & n > 4) %>%
  dplyr::ungroup() %>%
  dplyr::mutate(community = factor(community, levels = community[order(lr_mean)]))

bushfire_url_proj_c.df$community_fac <-
  bushfire_comm_ggplot_df$community[match(as.character(bushfire_url_proj_c.df$community),
                                          as.character(bushfire_comm_ggplot_df$community))]

bushfire_url_proj_c.df$lr_mean <-
  bushfire_comm_ggplot_df$lr_mean[match(as.character(bushfire_url_proj_c.df$community),
                                        as.character(bushfire_comm_ggplot_df$community))]


covid_comm_ggplot_df <- 
  covid_url_proj_c.df %>%
  dplyr::group_by(community) %>%
  dplyr::summarize(n = n(),
                   far_right_n = sum(far_right),
                   far_right_perc = far_right_n / n,
                   far_right_problematic_perc = sum(far_right & problematic_opinion)  / far_right_n,
                   journalist_n = sum(journalist),
                   journalist_perc = journalist_n / n,
                   journalist_problematic_perc = sum(journalist & problematic_opinion)  / journalist_n,
                   politician_n = sum(politician),
                   politician_perc = politician_n / n,
                   politician_problematic_perc = sum(politician & problematic_opinion)  / journalist_n,
                   problematic_n = sum(problematic_opinion),
                   problematic_perc = problematic_n / n,
                   lr_mean = mean(lr, na.rm = T)) %>%
  dplyr::filter(is.finite(lr_mean) & n > 4) %>%
  dplyr::ungroup() %>%
  dplyr::mutate(community = factor(community, levels = community[order(lr_mean)]))

covid_url_proj_c.df$community_fac <-
  covid_comm_ggplot_df$community[match(as.character(covid_url_proj_c.df$community),
                                       as.character(covid_comm_ggplot_df$community))]

covid_url_proj_c.df$lr_mean <-
  covid_comm_ggplot_df$lr_mean[match(as.character(covid_url_proj_c.df$community),
                                     as.character(covid_comm_ggplot_df$community))]

require(ggrepel)  

       cowplot::plot_grid(
         ggplot() +
           geom_jitter(data = 
                         bushfire_url_proj_c.df %>%
                         dplyr::filter(far_right & !is.na(community_fac)), 
                       aes(x = lr_mean, y = community_fac, shape = "far right account"),
                       colour = "red", width = 0.025, height = .8) +
           geom_point(data = bushfire_comm_ggplot_df %>%
                        dplyr::filter(!is.na(community)), 
                      aes(x = lr_mean, y = community, size = n), alpha = .2) +
           geom_label_repel(data = bushfire_comm_ggplot_df %>%
                              dplyr::filter(!is.na(community) & n > 50),
                            aes(label = paste0(round(n / nrow(bushfire_url_proj_c.df)*100,2),"%"),
                                x = lr_mean, y = community),
                            nudge_y = 4, point.padding = 3) +
           scale_size_continuous(range = c(1, 35)) +
           scale_y_discrete(drop=FALSE) +
           scale_x_continuous(limits = c(-1, -.4)) +
           theme_bw() +
           labs(x = "<= more left-wing   more right-wing =>", y = NULL, shape = NULL, 
                size = "community size") +
           theme(axis.ticks.y = element_blank(),
                 axis.text.y = element_blank(),
                 panel.grid.major.y = element_blank()) +
           guides(shape = FALSE),
         
         ggplot() +
           geom_jitter(data = 
                         covid_url_proj_c.df %>%
                         dplyr::filter(far_right & !is.na(community_fac)), 
                       aes(x = lr_mean, y = community_fac, shape = "far right account"),
                       colour = "red", width = 0.025, height = 1.5) +
           geom_point(data = covid_comm_ggplot_df %>%
                        dplyr::filter(!is.na(community)), 
                      aes(x = lr_mean, y = community, size = n), alpha = .2) +
           geom_label_repel(data = covid_comm_ggplot_df %>%
                              dplyr::filter(!is.na(community) & n > 500),
                            aes(label = paste0(round(n / nrow(covid_url_proj_c.df)*100,2),"%"),
                                x = lr_mean, y = community),
                            nudge_y = 8, point.padding = 3) +
           scale_size_continuous(range = c(1, 35)) +
           scale_x_continuous(limits = c(-1, -.4)) +
           scale_y_discrete(drop=FALSE) +
           theme_bw() +
           labs(x = "<= more left-wing   more right-wing =>", y = NULL, shape = NULL, 
                size = "community size") +
           theme(axis.ticks.y = element_blank(),
                 axis.text.y = element_blank(),
                 panel.grid.major.y = element_blank(),
                 legend.title = NULL),
         
         ncol = 1, labels = c(sprintf("Bushfires (n users = %s)", 
                                     format(nrow(bushfire_url_proj_c.df), big.mark=",", scientific=FALSE)), 
                              sprintf("Covid-19 (n users = %s)", 
                                     format(nrow(covid_url_proj_c.df), big.mark=",", scientific=FALSE))))

```

\clearpage

```{r domain_g_outdegree, include = T, fig.width = 10, fig.height = 7, fig.cap = "Outdegree distribution of the user-to-domain network."}

degree_dist.df <- 
  data.frame(prop.table(table(V(bushfire_user_domain.g)$outdegree[
    V(bushfire_user_domain.g)$type == TRUE]))) %>%
  
  dplyr::full_join(data.frame(prop.table(table(V(covid_user_domain.g)$outdegree[
    V(covid_user_domain.g)$type == TRUE]))),
    by = "Var1") 

degree_dist.df$diff <- 
  1 - degree_dist.df$Freq.y / degree_dist.df$Freq.x


       
       cowplot::plot_grid(
         
         degree_dist.df %>%
           dplyr::select(-diff) %>%
           tidyr::pivot_longer(cols = c(Freq.x, Freq.y)) %>%
           dplyr::mutate(Var1 = case_when(as.numeric(as.character(Var1))<30 ~
                                            as.character(Var1),
                                          as.numeric(as.character(Var1))>=30 ~ 
                                            "30+")) %>%
           dplyr::group_by(Var1, name) %>%
           dplyr::summarise(value = sum(value, na.rm = T)) %>%
           dplyr::mutate(Var1 = factor(Var1, levels = 
                                         c(as.character(c(1:29)), "30+"))) %>%
           ggplot(aes(x = Var1, y = value, fill = name)) +
           geom_bar(position = 'dodge', stat = 'identity') +
           theme_bw() +
           theme(legend.position = 'bottom') +
           scale_y_continuous(label = scales::percent) +
           labs(x = "linked domains", y = "users", fill = NULL) +
           scale_fill_manual(labels = c("Bushfires", "Covid-19"), 
                             values = c("orange", 'blue')),
         
         degree_dist.df %>%
           dplyr::select(-diff) %>%
           dplyr::mutate(Var1 = case_when(as.numeric(as.character(Var1))<30 ~
                                            as.character(Var1),
                                          as.numeric(as.character(Var1))>=30 ~ 
                                            "30+")) %>%
           dplyr::mutate(Var1 = factor(Var1, levels = 
                                         c(as.character(c(1:29)), "30+"))) %>%
           dplyr::group_by(Var1) %>%
           dplyr::summarise(Freq.x = sum(Freq.x, na.rm = T),
                            Freq.y = sum(Freq.y, na.rm = T)) %>%
           dplyr::ungroup() %>%
           dplyr::mutate(diff = 1 - Freq.y / Freq.x) %>%
           ggplot(aes(x = Var1)) +
           geom_bar(aes(y = diff), 
                    stat = 'identity')  +
           theme_bw() +
           scale_y_continuous(label = scales::percent) +
           labs(x = "linked domains", y = "proportional difference"),
         
         ncol = 1, align = 'v')
       
```

\clearpage

## Information disorder

```{r echo = FALSE}

# Renamed to avoid confusion: These are not only australian news sources but also global news sources

global_news_sources <- 
  australia_news_sources

```

The list of global news sources is created combined three collections compiled by the [Media Cloud](https://mediacloud.org/), an open source platform for crawling and indexing news stories. The Media Cloud collections we used are 

* "Australia National" (collection 34412282);
* "Australia State Local" (collection 38378024); and
* "Global English Language Sources" (collection 9272347).

This resulted in a list of `r nrow(global_news_sources)` news domains.


```{r results = "asis"}

global_news_sources %>%
  dplyr::group_by(pub_country) %>%
  dplyr::count() %>%
  dplyr::ungroup() %>%
  dplyr::top_n(n = 20, wt = n) %>%
  dplyr::arrange(desc(n)) %>%
  kbl(format = "latex", caption = "News sources: Top 20 countries of publication",
      booktabs = T) %>%
  kable_styling(latex_options = "HOLD_position")

```


```{r information_disorder, include = T, echo = TRUE, fig.cap = "Information disorder.", cache = T}

# Bushfire

bushfire_user_domain.el$news_source <- 
  global_news_sources$collection[match(bushfire_user_domain.el$domain,
                                          global_news_sources$domain)]

bushfire_user_domain.el$gov <- 
  grepl(".gov(.au)?$|.int$", bushfire_user_domain.el$domain)

bushfire_user_domain_info_disorder.el <-
  bushfire_user_domain.el %>%
  dplyr::select(user_id, created_at, domain, gov, news_source)

bushfire_user_domain_info_disorder.el$time_cut <-
  cut(bushfire_user_domain_info_disorder.el$created_at, "day")


# Covid

covid_user_domain.el$news_source <- 
  global_news_sources$collection[match(covid_user_domain.el$domain,
                                          global_news_sources$domain)]

covid_user_domain.el$gov <- 
  grepl(".gov(.au)?$|.int$", covid_user_domain.el$domain)

covid_user_domain_info_disorder.el <-
  covid_user_domain.el %>%
  dplyr::select(user_id, created_at, domain, gov, news_source)

covid_user_domain_info_disorder.el$time_cut <-
  cut(covid_user_domain_info_disorder.el$created_at, "day")

bushfire_user_domain_info_disorder.el %>%
  dplyr::distinct(user_id, domain, time_cut, news_source, gov) %>%
  dplyr::group_by(time_cut) %>%
  dplyr::summarise(unique_domains = length(unique(domain)) / length(unique(user_id)),
                   govnews_ratio = sum(gov | !is.na(news_source))  / n()) %>%
  dplyr::mutate(period = "Bushfires") %>%
  dplyr::bind_rows(
    covid_user_domain_info_disorder.el %>%
      dplyr::distinct(user_id, domain, time_cut, news_source, gov) %>%
      dplyr::group_by(time_cut) %>%
      dplyr::summarise(unique_domains = length(unique(domain)) / 
                         length(unique(user_id)),
                       govnews_ratio = sum(gov | !is.na(news_source))  / 
                         n()) %>%
                     dplyr::mutate(period = "Covid-19")) %>%
  ggplot() +
  geom_point(aes(x = govnews_ratio, y = unique_domains, colour = period),
             shape = 1, size = 2) +
  scale_x_continuous(label = percent) +
  scale_y_continuous(label = percent) +
  scale_colour_manual(labels = c("Bushfires", "Covid-19"), 
                      values = c("orange", 'blue')) +
  theme_bw() +
  labs(x = "government or news organisations' domains", 
       y = "unique domains per unique users", 
       colour = NULL)

```


\clearpage

# User-User network (mutual retweets)


```{r mutual_rt, include = T, fig.width = 15, fig.height = 10, fig.cap = "Mutual retweet networks.", cache = T}

bushfire_rt.df <- 
  bushfire_tweets.dt[!is.na(bushfire_tweets.dt$retweeted_status_user_id),
                     c("user_id", "retweeted_status_user_id")]

bushfire_mutual_rt.g <- 
  graph.data.frame(bushfire_rt.df) %>%
  as.undirected(mode = 'mutual')

E(bushfire_mutual_rt.g)$weight <- 1

bushfire_mutual_rt.g <- 
  bushfire_mutual_rt.g %>%
  igraph::simplify()

V(bushfire_mutual_rt.g)$degree <-
  igraph::degree(bushfire_mutual_rt.g, mode = 'total')

bushfire_mutual_rt.g <-
  bushfire_mutual_rt.g %>%
  igraph::delete.vertices(igraph::degree(bushfire_mutual_rt.g, mode = 'total') == 0)

V(bushfire_mutual_rt.g)$far_right <- 
  V(bushfire_mutual_rt.g)$name %in% farright_user_ids

V(bushfire_mutual_rt.g)$lr <- 
  bushfire_user_lr.df$lr_reuters_2020[match(V(bushfire_mutual_rt.g)$name,
                                            bushfire_user_lr.df$user_id)]

V(bushfire_mutual_rt.g)$far_right_weight <- 
  all_user_description_weight$weight[match(V(bushfire_mutual_rt.g)$name,
                                           all_user_description_weight$user_id)]

bushfire_layout <-
  bushfire_mutual_rt.g %>%
  igraph::delete.vertices(igraph::degree(bushfire_mutual_rt.g, mode = 'total') < 1) %>%
  ggnetwork()


covid_rt.df <- 
  covid_tweets.dt[!is.na(covid_tweets.dt$retweeted_status_user_id),
                  c("user_id", "retweeted_status_user_id")]

covid_mutual_rt.g <- 
  graph.data.frame(covid_rt.df) %>%
  as.undirected(mode = 'mutual')

E(covid_mutual_rt.g)$weight <- 1

covid_mutual_rt.g <- 
  covid_mutual_rt.g %>%
  igraph::simplify()

V(covid_mutual_rt.g)$degree <-
  igraph::degree(covid_mutual_rt.g, mode = 'total')

covid_mutual_rt.g <-
  covid_mutual_rt.g %>%
  igraph::delete.vertices(igraph::degree(covid_mutual_rt.g, mode = 'total') == 0)

V(covid_mutual_rt.g)$far_right <- 
  V(covid_mutual_rt.g)$name %in% farright_user_ids

V(covid_mutual_rt.g)$lr <- 
  covid_user_lr.df$lr_reuters_2020[match(V(covid_mutual_rt.g)$name,
                                         covid_user_lr.df$user_id)]
V(covid_mutual_rt.g)$betweenness <- 
  igraph::betweenness(covid_mutual_rt.g)

V(covid_mutual_rt.g)$far_right_weight <- 
  all_user_description_weight$weight[match(V(covid_mutual_rt.g)$name,
                                           all_user_description_weight$user_id)]

covid_layout <-
  decompose(covid_mutual_rt.g)[[1]] %>%
  ggnetwork()

# distance <- 
#   igraph::all_simple_paths(covid_mutual_rt.g,
#                            from = '15486485',
#                            to = farright_user_ids[
#                              farright_user_ids %in% 
#                                V(covid_mutual_rt.g)$name
#                            ])

       cowplot::plot_grid(
         
         ggplot() +
           geom_edges(data = bushfire_layout,
                      aes(x = x, y = y, xend = xend, yend = yend),
                      alpha = .95, color = "grey70", size = .1) +
           geom_nodes(data = bushfire_layout,
                      aes(x = x, y = y,
                          colour = lr, size = degree)) +
           theme_void() +
           scale_color_distiller(palette = "RdYlBu", limits = right_left_scale_limits) +
           lims(x = c(.25, .8), y = c(.25, .8)) +
           scale_size_continuous(range = c(.5, 5)) +
           labs(colour = "left-right index") +
           theme(legend.position = 'bottom') +
           guides(size = 'none', colour = 'none'),
         
         ggplot() +
           geom_edges(data = bushfire_layout,
                      aes(x = x, y = y, xend = xend, yend = yend),
                      alpha = .95, color = "grey70", size = .1) +
           geom_nodes(data = bushfire_layout,
                      aes(x = x, y = y,
                          size = degree), color = "grey70") +
           geom_nodes(data = bushfire_layout %>%
                        dplyr::distinct(x, y, .keep_all = T) %>%
                        dplyr::filter(far_right),
                      aes(x = x, y = y,
                          size = degree*10), colour = "black", alpha = .5) +
           theme_void() +
           scale_color_distiller(palette = "RdBu") +
           lims(x = c(.25, .8), y = c(.25, .8)) +
           scale_size_continuous(range = c(.5, 5)) +
           theme(legend.position = 'bottom'),
         
         # ggplot() +
         #   geom_edges(data = bushfire_layout,
         #              aes(x = x, y = y, xend = xend, yend = yend),
         #              alpha = .95, color = "grey70", size = .1) +
         #   geom_nodes(data = bushfire_layout,
         #              aes(x = x, y = y,
         #                  colour = far_right_weight, size = degree)) +
         #   theme_void() +
         #   scale_color_distiller(palette = "YlOrRd", direction = 1) +
         #   lims(x = c(.20, .7), y = c(.4, .7)) +
         #   scale_size_continuous(range = c(.5, 5)) +
         #   labs(colour = "far-right index"),
         
         
         ggplot() +
           geom_edges(data = covid_layout,
                      aes(x = x, y = y, xend = xend, yend = yend),
                      alpha = .95, color = "grey70", size = .1) +
           geom_nodes(data = covid_layout,
                      aes(x = x, y = y,
                          colour = lr, size = degree)) +
           theme_void() +
           scale_color_distiller(palette = "RdYlBu", limits = right_left_scale_limits) +
           scale_size_continuous(range = c(.5, 5))  +
           labs(colour = "left-right index") +
           theme(legend.position = 'bottom') +
           guides(size = 'none') +
           guides(color = guide_colourbar(title = "<= left  right =>", 
                                          title.position="top", 
                                          title.hjust = 0.5,
                                          labels = NULL)),
         
         ggplot() +
           geom_edges(data = covid_layout,
                      aes(x = x, y = y, xend = xend, yend = yend),
                      alpha = .95, color = "grey70", size = .1) +
           geom_nodes(data = covid_layout,
                      aes(x = x, y = y,
                          size = degree), color = "grey70") +
           geom_nodes(data = covid_layout %>%
                        dplyr::distinct(x, y, .keep_all = T) %>%
                        dplyr::filter(far_right),
                      aes(x = x, y = y,
                          size = degree*20), colour = "black", alpha = .5) +
           theme_void() +
           scale_color_distiller(palette = "RdBu") +
           scale_size_continuous(range = c(.5, 5)) +
           theme(legend.position = 'bottom'),
         
         # ggplot() +
         #   geom_edges(data = covid_layout,
         #              aes(x = x, y = y, xend = xend, yend = yend),
         #              alpha = .95, color = "grey70", size = .1) +
         #   geom_nodes(data = covid_layout,
         #              aes(x = x, y = y,
         #                  colour = far_right_weight, size = degree)) +
         #   theme_void() +
         #   scale_color_distiller(palette = "YlOrRd", direction = 1) +
         #   scale_size_continuous(range = c(.5, 5)) +
         #   labs(colour = "far-right index"),
         
         ncol = 2, labels = c(sprintf("Bushfires (n users = %s)", 
                                      format(vcount(bushfire_mutual_rt.g), 
                                             big.mark=",", scientific=FALSE)), 
                              sprintf("far-right accounts (n = %s)", 
                                      format(sum(V(bushfire_mutual_rt.g)$name %in% farright_user_ids), 
                                             big.mark=",", scientific=FALSE)),
                              sprintf("Covid-19 (n users = %s)", 
                                      format(vcount(covid_mutual_rt.g), 
                                             big.mark=",", scientific=FALSE)), 
                              sprintf("far-right accounts (n = %s)", 
                                      format(sum(V(covid_mutual_rt.g)$name %in% farright_user_ids), 
                                             big.mark=",", scientific=FALSE))))
       
```

```{r include = T, results = 'asis'}

data.frame(nodes = c(vcount(bushfire_mutual_rt.g %>%
  igraph::delete.vertices(igraph::degree(bushfire_mutual_rt.g, mode = 'total') < 1)), vcount(decompose(covid_mutual_rt.g)[[1]])),
           edges = c(ecount(bushfire_mutual_rt.g %>%
  igraph::delete.vertices(igraph::degree(bushfire_mutual_rt.g, mode = 'total') < 1)), ecount(decompose(covid_mutual_rt.g)[[1]])),
           type = c("user-user", "user-user"),
           period = c("Period 1", "Period 2")) %>%
  kbl(format = "latex", caption = "User-User network",
      booktabs = T)
```


\clearpage

```{r mutual_rt_comm, include = T,  fig.width = 14, fig.height = 6, fig.cap = "Communities of the mutual retweet network.", cache = T}

library(graphlayouts)
library(ggraph)

bushfire_mutual_rt.c <-
  igraph::cluster_fast_greedy(bushfire_mutual_rt.g)

covid_mutual_rt.c <-
  igraph::cluster_fast_greedy(covid_mutual_rt.g)

V(bushfire_mutual_rt.g)$community <- 
  bushfire_mutual_rt.c$membership[match(V(bushfire_mutual_rt.g)$name,
                                        bushfire_mutual_rt.c$names)]

bushfire_mutual_rt_comm.g <- 
  igraph::contract(bushfire_mutual_rt.g %>%
                     igraph::delete_edge_attr('weight'), 
                   factor(V(bushfire_mutual_rt.g)$community),
                   vertex.attr.comb=list(community="first",
                                         name="ignore"))

V(bushfire_mutual_rt_comm.g)$name <- 
  V(bushfire_mutual_rt_comm.g)$community

E(bushfire_mutual_rt_comm.g)$weight <- 1

bushfire_mutual_rt_comm.g <- 
  igraph::simplify(bushfire_mutual_rt_comm.g) %>%
  igraph::as_adj(attr = 'weight') %>%
  igraph::graph_from_adjacency_matrix(weighted=TRUE)


bushfire_mutual_rt.df <- 
  data.frame(user_id = bushfire_mutual_rt.c$names,
             community = bushfire_mutual_rt.c$membership) %>%
  dplyr::mutate(far_right = user_id %in% farright_user_ids,
                lr = bushfire_user_lr.df$lr_reuters_2020[match(user_id,
                                                               bushfire_user_lr.df$user_id)],
                far_right_weight =
                  all_user_description_weight$weight[match(user_id,
                                                           all_user_description_weight$user_id)])

bushfire_mutual_rt_comm.df <-
  bushfire_mutual_rt.df %>%
  dplyr::group_by(community) %>%
  dplyr::summarise(n = n(),
                   n_perc = round(n / nrow(bushfire_mutual_rt.df) * 100, 2),
                   far_right_n = sum(far_right),
                   lr_mean = mean(lr, na.rm = T),
                   far_right_weight_mean = mean(far_right_weight, na.rm = T),
                   far_right_perc = far_right_n / n) 

for (i in 2:7) {
  
  this_colname <- 
    colnames(bushfire_mutual_rt_comm.df)[i]
  
  bushfire_mutual_rt_comm.g <- 
    bushfire_mutual_rt_comm.g %>%
    set_vertex_attr(this_colname, 
                    value = 
                      bushfire_mutual_rt_comm.df[[this_colname]][
                        match(V(bushfire_mutual_rt_comm.g)$name,
                              as.character(bushfire_mutual_rt_comm.df$community))])
  
  
  
}

covid_mutual_rt.c <-
  igraph::cluster_fast_greedy(covid_mutual_rt.g)


V(covid_mutual_rt.g)$community <- 
  covid_mutual_rt.c$membership[match(V(covid_mutual_rt.g)$name,
                                     covid_mutual_rt.c$names)]

covid_mutual_rt_comm.g <- 
  igraph::contract(covid_mutual_rt.g, 
                   factor(V(covid_mutual_rt.g)$community),
                   vertex.attr.comb=list(community="first",
                                         name="ignore"))

V(covid_mutual_rt_comm.g)$name <- 
  V(covid_mutual_rt_comm.g)$community

E(covid_mutual_rt_comm.g)$weight <- 1

# igraph::degree(covid_mutual_rt_comm.g)[V(covid_mutual_rt_comm.g)$name == 8] / 2

covid_mutual_rt_comm.g <- 
  igraph::simplify(covid_mutual_rt_comm.g) %>%
  igraph::as_adj(attr = 'weight') %>%
  igraph::graph_from_adjacency_matrix(weighted=TRUE)


covid_mutual_rt.df <- 
  data.frame(user_id = covid_mutual_rt.c$names,
             community = covid_mutual_rt.c$membership) %>%
  dplyr::mutate(far_right = user_id %in% farright_user_ids,
                lr = covid_user_lr.df$lr_reuters_2020[match(user_id,
                                                            covid_user_lr.df$user_id)],
                far_right_weight =
                  all_user_description_weight$weight[match(user_id,
                                                           all_user_description_weight$user_id)])

covid_mutual_rt_comm.df <-
  covid_mutual_rt.df %>%
  dplyr::group_by(community) %>%
  dplyr::summarise(n = n(),
                   n_perc = round(n / nrow(covid_mutual_rt.df) * 100, 2),
                   far_right_n = sum(far_right),
                   lr_mean = mean(lr, na.rm = T),
                   far_right_weight_mean = mean(far_right_weight, na.rm = T),
                   far_right_perc = far_right_n / n) 

for (i in 2:7) {
  
  this_colname <- 
    colnames(covid_mutual_rt_comm.df)[i]
  
  covid_mutual_rt_comm.g <- 
    covid_mutual_rt_comm.g %>%
    set_vertex_attr(this_colname, 
                    value = 
                      covid_mutual_rt_comm.df[[this_colname]][
                        match(V(covid_mutual_rt_comm.g)$name,
                              as.character(covid_mutual_rt_comm.df$community))])
  
  
  
}

bushfire_mutual_rt_comm.layout <- 
  bushfire_mutual_rt_comm.g %>%
  # igraph::delete.vertices(igraph::degree(bushfire_mutual_rt_comm.g) < 2 &
  #                          V(bushfire_mutual_rt_comm.g)$far_right_n < 10) %>%
  ggnetwork(layout = igraph::layout_with_kk(.)) %>%
  dplyr::mutate(far_right_bool = far_right_n > 10)

covid_mutual_rt_comm.layout <- 
  covid_mutual_rt_comm.g %>%
  # igraph::delete.vertices(igraph::degree(covid_mutual_rt_comm.g) == 0 &
  #                          V(covid_mutual_rt_comm.g)$far_right_n < 10) %>%
  igraph::set_edge_attr(name = "weight", value = sqrt(E(.)$weight)) %>%
  ggnetwork(layout = igraph::layout_with_kk(.)) %>%
  dplyr::mutate(far_right_bool = far_right_n > 10)


legend <- 
  cowplot::get_legend(
    ggplot(bushfire_mutual_rt_comm.layout, 
           aes(x = x, y = y, xend = xend, yend = yend)) +
      geom_edges(size = .5, colour = 'gray80') +
      geom_nodes(aes(size = n, colour = lr_mean)) +
      geom_nodelabel_repel(data = bushfire_mutual_rt_comm.layout %>%
                             dplyr::filter(far_right_bool), 
                           label = "far-right",
                           force = 100) +
      scale_size_continuous(range = c(1.5, 10)) +
      scale_colour_distiller(palette = "RdYlBu",
                             limits = right_left_scale_limits) +
      theme_blank() +
      theme(legend.position = 'bottom') +
      guides(color = guide_colourbar(title = "<= left  right =>", 
                                     title.position="top", 
                                     title.hjust = 0.5,
                                     labels = NULL)) +
      guides(size = 'none')
  )

cowplot::plot_grid(
  
  cowplot::plot_grid(
    
    ggplot(bushfire_mutual_rt_comm.layout, 
           aes(x = x, y = y, xend = xend, yend = yend)) +
      geom_edges(size = .5, colour = 'gray80') +
      geom_nodes(aes(size = n, colour = lr_mean)) +
      geom_nodelabel_repel(data = bushfire_mutual_rt_comm.layout %>%
                             dplyr::filter(far_right_bool), 
                           label = "far-right",
                           force = 100) +
      scale_size_continuous(range = c(1.5, 10)) +
      scale_colour_distiller(palette = "RdYlBu",
                             limits = right_left_scale_limits) +
      theme_blank() +
      guides(colour = "none") +
      labs(size = "community\nsize"),
    
    legend, ncol = 1, rel_heights = c(1,.15)),
  
  ggplot(covid_mutual_rt_comm.layout, 
         aes(x = x, y = y, xend = xend, yend = yend)) +
    geom_edges(size = .5, colour = 'gray80') +
    geom_nodes(aes(size = n, colour = lr_mean)) +
    geom_nodelabel_repel(data = covid_mutual_rt_comm.layout %>%
                           dplyr::filter(far_right_bool), 
                         label = "far-right",
                         force = 300) +
    scale_size_continuous(range = c(1.5, 10)) +
    scale_colour_distiller(palette = "RdYlBu", 
                           limits = right_left_scale_limits) +
    theme_blank() +
    guides(colour = 'none') +
      labs(size = "community\nsize"),
  ncol = 2, labels = c("Bushfires", "Covid-19"), align = "vh")

```





